<?php

require_once(DAEMONE_HOME.'/lib/HTTP/Client.php');


function html_retrieve($url, &$config=NULL)
{
	
	try 
	{
		$client = new HTTP_CLIENT();
		// $client->setDefaultHeader("User-Agent", 'compatible; Mozilla/5.0; GoogleBot; FAST Crawler 6.4; http://www.araby.com;');
		$client->setMaxRedirects(2);	
		$client->get($url);	
		return $client->currentResponse();
		
	}
	catch (Exception $e) 
	{
		$response['error'] = $e->getError();
		return $response;
	}
	
}

function html_retrieve_ezin_article($url)
{
	$config['resolveExternals'] = FALSE;
	$config['strictErrorChecking'] = FALSE;
	$config['recover'] = TRUE;
	
	try 
	{
		$html_response = html_retrieve($url);
	    $response['original_url'] = $url;
		
		// fail to reterive 
		if ($html_response['code'] != 200)
		{
			
		   $response['status'] = 'FAIL_TO_GET_DOC';
		   $response['http_code'] = $html_response['code'];
		   $response['html_body'] = $html_response['body'];
		   return $response;
		}
		
	    $response['article_original_url'] = $html_response['url'];
	    $response['http_code'] = $html_response['code'];
		$response['html_body'] = $html_response['body'];
		
		$dom_documnet = new DOMDocument();
		if (!$dom_documnet->loadHTML($html_response['body']))
		{
		   $response['status'] = 'DOM_BODY_PARSE_FAIL';
		   return $response;
		}
		
		$xpath = new DOMXpath($dom_documnet);

		// parse article content
		$elements = $xpath->query("//div[@id='article-content']");
		
		$html_article = '';
		if (!is_null($elements) && $elements->length > 0) 
		{	
			$element = $elements->item(0);
			$html_article = $dom_documnet->saveXML($element);
		}
		
		if($html_article == '')
		{
			$response['status'] = 'FAIL_TO_PASRE_DOC';
			$response['http_code'] = $html_response['code'];
			$response['html_body'] = $html_response['body'];
			return $response;
		}
		$response['html_article'] = $html_article;
		
		// parse html meta keywords
		$elements = $xpath->query("*/meta[@name='keywords']");
		$html_meta_keywords = '';
		if (!is_null($elements)) 
		{	
			foreach ($elements as $element) 
			{
				$length = $element->attributes->length;
				for ($i = 0; $i < $length; ++$i) 
				{
					$name = $element->attributes->item($i)->name;
					if($name == 'content')
						$html_meta_keywords = trim($element->attributes->item($i)->value);
					
				}
			}
		}
		$response['html_meta_keywords'] = $html_meta_keywords;
		
		// parse html meta description
		$elements = $xpath->query("*/meta[@name='description']");
		$html_meta_description = '';
		if (!is_null($elements)) 
		{	
			foreach ($elements as $element) 
			{
				$length = $element->attributes->length;
				for ($i = 0; $i < $length; ++$i) 
				{
					$name = $element->attributes->item($i)->name;
					if($name == 'content')
						$html_meta_description = trim($element->attributes->item($i)->value);
					
				}
			}
		}
		$response['html_meta_description'] = $html_meta_description;
		
		// parse author name
		$elements = $xpath->query("//div[@class='author-profile']/a[@class='author-name']");
		$html_author_name = '';
		if (!is_null($elements) && $elements->length > 0) 
		{	
			$element = $elements->item(0);
			$html_author_name = trim($element->textContent);
		}
		$response['html_author_name'] = $html_author_name;

		// parse author url
		$html_author_url = '';
		if (!is_null($elements) && $elements->length > 0) 
		{	
			foreach ($elements as $element) 
			{
				$length = $element->attributes->length;
				for ($i = 0; $i < $length; ++$i) 
				{
					$name = $element->attributes->item($i)->name;
					if($name == 'href')
						$html_author_url = trim($element->attributes->item($i)->value);
					
				}
			}
		}
		$response['html_author_url'] = $html_author_url;

		// parse membership type
		$elements = $xpath->query("//span[@class='membership-level']");
		$html_author_membership_type = '';
		if (!is_null($elements) && $elements->length > 0) 
		{	
			$element = $elements->item(0);
			$html_author_membership_type = trim($element->textContent);
		}
		$response['html_author_membership_type'] = $html_author_membership_type;
		

		// parse author country
		$elements = $xpath->query("//span[@class='joined']/img");
		$html_author_country = '';
		if (!is_null($elements) && $elements->length > 0) 
		{	
			foreach ($elements as $element) 
			{
				$length = $element->attributes->length;
				for ($i = 0; $i < $length; ++$i) 
				{
					$name = $element->attributes->item($i)->name;
					if($name == 'title')
						$html_author_country = $element->attributes->item($i)->value;
					
				}
			}
		}
		$response['html_author_country'] = $html_author_country;

		// parse similiar articles
		$elements = $xpath->query("//ul[@class='ea-similar-articles ea-article-list']/li/a");
		$html_similiar_articles = NULL;
		$j = 0;
		if (!is_null($elements) && $elements->length > 0) 
		{	
			foreach ($elements as $element) 
			{
				$html_similiar_articles[$j]['related_article_title'] = trim($element->textContent);
				$length = $element->attributes->length;
				for ($i = 0; $i < $length; ++$i) 
				{
					$name = $element->attributes->item($i)->name;
					if($name == 'href')
						$html_similiar_articles[$j]['related_article_url'] = $element->attributes->item($i)->value;
					
				}
				$j++; 
			}
		}
		$response['html_similiar_articles'] = $html_similiar_articles;
		$response['status'] = 'SUCCESS';
	}
	catch (Exception $e) 
	{
		$response['error']= json_encode($e);
	}
	
	return $response;
}

?>
